const { Category } = require('../models');

// 获取所有分类
exports.getCategories = async (req, res, next) => {
    try {
        const categories = await Category.findAll({
            attributes: ['id', 'name', 'description']
        });
        
        res.json({
            success: true,
            data: categories
        });
    } catch (error) {
        next(error);
    }
};

// 获取单个分类
exports.getCategoryById = async (req, res, next) => {
    try {
        const { id } = req.params;
        
        const category = await Category.findByPk(id, {
            attributes: ['id', 'name', 'description']
        });
        
        if (!category) {
            return res.status(404).json({
                success: false,
                message: '分类不存在'
            });
        }
        
        res.json({
            success: true,
            data: category
        });
    } catch (error) {
        next(error);
    }
};